/*---------------------------------------------------------------------
 * File name: dispaly.h
 *
 * Copyright (c) <2020-2022>, <ChenLong>
 *
 * All rights reserved.
 *
 * Author: ChenLong
 * Email: worldlong@foxmail.com
 *--------------------------------------------------------------------*/
#pragma once
/*
*/
#include "font.h"
/*
*/
typedef struct
{
  int x0;
  int x1;
  int y0;
  int y1;
  const void *data;
  int size;
}print_info_t;
/*
*/
class DisplayDev
{
public:
  DisplayDev(){};
  virtual void init() = 0;
  virtual void clear() = 0;
  virtual void set_pos(int x0, int x1, int y0, int y1) = 0;
  virtual void print(print_info_t &info) = 0;
  virtual void reset() = 0;
  virtual void display_on() = 0;
  virtual void display_off() = 0;   
};
/*
*/
template<typename Type>
class TextLine
{
public:
  TextLine(DisplayDev *dev, int x0, int height, int y0, int width) :
    _x0(x0),
    _x1(x0 + (height + sizeof(Type)*8 - 1)/(sizeof(Type)*8) - 1),
    _y0(y0),
    _width(width),
    _height(height),
    _size(height*width/(sizeof(Type)*8)),
    _index(0),
    _number(0),
    _dev(dev)
    {}
    
  void print(Type *data)
  {
    print_info_t text;
    text.x0 = _x0;
    text.x1 = _x1;
    text.y0 = _y0 + _index * _width;
    text.y1 = text.y0 + _width - 1;
    text.size = _size;
    text.data = data;
    _index++;
    if(_number < _index) {
      _number = _index;
    }
    _dev->print(text);
  }
  
  void set_pos(int pos)
  {
    _index = pos;
  }
  
  void back_space()
  {
    if(_index == 0) {
      return;
    }
    
    _index--;
    print(0);
    if(_index == _number) {
      _index--;
      _number--;
    }
  }
  
  void clear_res()
  {
    int index = _index;
    for(int i = _index; i<_number; i++) {
      print(0);
    }
    _index = index;
    _number = index;
  }
  
  int size() {return _size;}
private:
  int _x0;
  int _x1;
  int _y0;
  int _width;
  int _height;
  int _size;
  int _index;
  int _number;
  DisplayDev *_dev;
};
/*
*/
#include "ZJY096S0700BG01.h"







